package cn.iocoder.yudao.module.nmkj.service.newscontent;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.nmkj.controller.admin.newscontent.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.newscontent.NewsContentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nmkj.dal.mysql.newscontent.NewsContentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NEWS_CONTENT_NOT_EXISTS;

/**
 * 新闻内容 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class NewsContentServiceImpl implements NewsContentService {

    @Resource
    private NewsContentMapper newsContentMapper;

    @Override
    public Long createNewsContent(NewsContentSaveReqVO createReqVO) {
        // 插入
        NewsContentDO newsContent = BeanUtils.toBean(createReqVO, NewsContentDO.class);
        newsContentMapper.insert(newsContent);
        // 返回
        return newsContent.getId();
    }

    @Override
    public void updateNewsContent(NewsContentSaveReqVO updateReqVO) {
        // 校验存在
        validateNewsContentExists(updateReqVO.getId());
        // 更新
        NewsContentDO updateObj = BeanUtils.toBean(updateReqVO, NewsContentDO.class);
        newsContentMapper.updateById(updateObj);
    }

    @Override
    public void deleteNewsContent(Long id) {
        // 校验存在
        validateNewsContentExists(id);
        // 删除
        newsContentMapper.deleteById(id);
    }

    private void validateNewsContentExists(Long id) {
        if (newsContentMapper.selectById(id) == null) {
            throw exception(NEWS_CONTENT_NOT_EXISTS);
        }
    }

    @Override
    public NewsContentDO getNewsContent(Long id) {
        return newsContentMapper.selectById(id);
    }

    @Override
    public PageResult<NewsContentDO> getNewsContentPage(NewsContentPageReqVO pageReqVO) {
        return newsContentMapper.selectPage(pageReqVO);
    }

    @Override
    public NewsContentDO getNewsContentApp(Long id) {
        return newsContentMapper.selectById(id);
    }

}